package rickyGame.game.action.ai
{
	import flash.geom.Point;
	
	import ricky.animate.FrameManager;
	import ricky.animate.TimeServer;
	import ricky.game.Actor;
	import ricky.game.action.Act2D;
	import ricky.quadtree.Leaf;
	import ricky.quadtree.Morton;
	import ricky.quadtree.Quadtree;
	
	import rickyGame.game.GameState;
	import rickyGame.game.Player;
	import rickyGame.game.scene.Pathfinder;
	
	/**
	 * 躲避玩家
	 */
	public class EvadeAi extends WanderAi
	{
		public function EvadeAi(actor:Actor, par:Object=null)
		{
			super(actor,par);
		}

		/**
		 * 寻找范围内可以到达的路径
		 */
		override protected function findTarget():Boolean
		{
			var l:Leaf;
			
			var gridx:int=_localX/40;
			var gridy:int=_localY/30;
			var me:Player=GameState.inst.me;
			
			var dirx:int=this.actor.x-me.X;
			var diry:int=this.actor.y-me.Y;
				
			var tx:int=gridx+(Math.random()*(dirx/10)*_range+1);
			var ty:int=gridy+(Math.random()*(diry/10)*_range+1);
			
			if(tx<=0)
				tx=0;
			if(ty<=0)
				ty=0;

			var m:int=Morton.morton(ty,tx);
			
			l=GameState.inst.scene.tree.getLeaf(m);
			
			if(l)
			{
				var tree:Quadtree=GameState.inst.scene.tree;
				var ml:Leaf=tree.getLeaf(_actor.morton);
				var arr:Array=Pathfinder.findPath1(ml,l,tree,Pathfinder.traversableAroundFunction,_actor.asType);
				_path=arr;
				_isfindTarget= true;
				this._animProperty="run";
			}
			else
			{
				_isfindTarget= false;
				_animProperty="stand";
			}
			
			return _isfindTarget;
		}
	}
}